package com.hxy.health.platform.controller;

import com.github.pagehelper.PageInfo;
import com.hxy.health.platform.pojo.Blog;
import com.hxy.health.platform.pojo.Comment;
import com.hxy.health.platform.service.BlogService;
import com.hxy.health.platform.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;
    /**
     * 跳转到博客界面
     */
    @RequestMapping("page/blog")
    public String blogPage(ModelMap modelMap, @RequestParam(value="pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<Blog> blogs = blogService.getAll(pageNum, pageSize);
        modelMap.addAttribute("blogs", blogs);
        return "blog";
    }

    /**
     * 跳转到博客明细界面
     */
    @RequestMapping("page/blogDetail")
    public String blogDetailPage(@RequestParam("id") Integer id,
                                 @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,

                                 ModelMap modelMap) {
        Blog blog = blogService.getById(id);
        modelMap.addAttribute("blog", blog);
        PageInfo<Blog> blogs = blogService.getAll(pageNum, pageSize);
        modelMap.addAttribute("blogs", blogs);
        PageInfo<Comment> comment = commentService.getByArticleId(pageNum, pageSize, id);
        modelMap.addAttribute("comment", comment);
        return "blogDetail";
    }
}
